/*********************************************************************\
*
*                  (c) lkc
*                  All Rights Reserved. lkc Confidential
*
\*********************************************************************/
/*********************************************************************\
*   Title                      :
*
*   Module Description         :
*
*   Author                     : lkc
*
*   Created                    : 2019-11-12
*
\*********************************************************************/

#include "ProtocolPodXinWang.h"
#include "UtilityTH.h"

ProtocolPodXinWang ProtocolPodXinWang::m_Instance;//静态成员变量在类中仅仅是声明，没有定义，所以要在类的外面定义，实际上是给静态成员变量分配内存

int ProtocolPodXinWang::Passthrough(const unsigned char *sendBuf,int dataLen)
{
    return Send(sendBuf,dataLen);
}

void ProtocolPodXinWang::ExtractData(const unsigned char *data, int nLength)
{
    m_bufferTempMtx.lock();
    m_bufferTemp.insert(m_bufferTemp.end(),data,data+nLength);
    while( m_bufferTemp.size() >= FRAME_LENGTH_RECEIVE_EVENT)
    {
        auto iter = m_bufferTemp.begin();
        if((*iter == HEAD1_CYCLE)&&(*(iter+1) == HEAD2_CYCLE))
        {
            if(m_bufferTemp.size()< FRAME_LENGTH_RECEIVE_CYCLE)
            {
                continue;
            }

            if(*(iter+FRAME_LENGTH_RECEIVE_CYCLE-1) != END_CYCLE)
            {
                m_bufferTemp.erase(iter,iter+2);
                continue;
            }
            unsigned char sum = CheckSum(iter+2,6);
            if(*(iter+FRAME_LENGTH_RECEIVE_CYCLE-2) != sum)
            {
                m_bufferTemp.erase(iter,iter+2);
                continue;
            }
            CycleReceiveFrameParse();
            m_bufferTemp.erase(iter,iter+FRAME_LENGTH_RECEIVE_CYCLE);
        }
        else if((*iter == HEAD1_EVENT)&&(*(iter+1) == HEAD2_EVENT))
        {
            unsigned char sum = CheckSum(iter+1,5);
            if(*(iter+FRAME_LENGTH_RECEIVE_EVENT-1) != sum)
            {
                m_bufferTemp.erase(iter,iter+2);
                continue;
            }
            EventReceiveFrameParse();
            m_bufferTemp.erase(iter,iter+FRAME_LENGTH_RECEIVE_EVENT);
        }
        else
        {
            //如果数据开始不是头，则删除数据
            m_bufferTemp.erase(iter);
            continue;
        }
    }
    m_bufferTempMtx.unlock();
}

unsigned char ProtocolPodXinWang::GetMode()
{



    return m_mode;
}

int ProtocolPodXinWang::GetSelfCheck()
{



    return m_selfCheck;
}

void ProtocolPodXinWang::EventReceiveFrameParse()
{
    printf("EventReceiveFrameParse\n");

    unsigned char command2 = m_bufferTemp[3];
    float angle = ((m_bufferTemp[5]<<8)+m_bufferTemp[5])/100.0;
    if(command2==0x59)//航向角
    {
        m_yaw = angle;
    }
    else if(command2==0x5B)//俯仰角
    {
        m_pitch = angle;
    }
}


void ProtocolPodXinWang::CycleReceiveFrameParse()
{
//    printf("CycleReceiveFrameParse\n");
    m_mode = m_bufferTemp[2];
    m_pitch = ((m_bufferTemp[3]<<8)+m_bufferTemp[4])/100.0;
    m_yaw = ((m_bufferTemp[5]<<8)+m_bufferTemp[6])/100.0;
    m_selfCheck = m_bufferTemp[7];
}

void ProtocolPodXinWang::SelfCheckParse()
{
}


/********************************************************************\
 *
 * REVISION RECORDS
 *
\*********************************************************************/
/*********************************************************************/
/*
 *
 *
 *
\*********************************************************************/
/*------------------------------The End------------------------------*/
